package service;

import entity.Reader;
import function.Druid;
import function.ReaderDaoImpl;
import function.ReaderTypeDaoImpl;
import interfaces.FineDao;
import tool.MyTool;
import tool.TSUtility;
import tool.YouTool;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Properties;
import java.util.Set;

public class FineServiceImpl implements FineDao {

    @Override
    public void insert() {
        Connection connection = null;
        try {
            connection = Druid.getConnection();
            ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
            ReaderServiceDaoImpl readerServiceDao = new ReaderServiceDaoImpl();
            System.out.println("目前所拥有的读者类型");

            List<Reader> all = readerDao.getAll(connection);
            MyTool.printArr(all);
            Integer typeIndex = MyTool.getTypeIndex(all.size());
            String readerid = all.get(typeIndex).getReaderid();
            System.out.println("-----------------------");
            //***********************************************
            //判断有无该读者
            Reader reader = readerServiceDao.judgmentExists(readerid);
            if (reader == null) {
                System.out.println("暂无读者类型，先前往设置");
                System.out.println("----------------------");
                return;
            }
            boolean b = judgmentExist(readerid);
            if (b == true) {
                System.out.println("该读者已经有罚金，无法添加");
                return;
            }

            System.out.println("输入你的罚金——(0-99)");
            int fine = TSUtility.readInt();
            Properties po = new Properties();
            po.setProperty(readerid, String.valueOf(fine));
            FileWriter fw = new FileWriter("src//src//File//fineType.txt",true);
            po.store(fw, null);
            System.out.println("添加成功");
            fw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Druid.colseResource(connection, null, null);
        }
    }

    public boolean judgmentExist(String readerID) throws IOException {

        Properties po = new Properties();
        //该读者是否有罚金类型
        FileReader fr = new FileReader("src//src//File//fineType.txt");
        po.load(fr);
        Set<String> strings = po.stringPropertyNames();
        for (String key : strings) {
            if ((readerID.equals(key))) {
                fr.close();
                return true;
            }
        }
        fr.close();
        return false;
    }
    @Override
    public void deleteById() {
        ReaderTypeDaoImpl readerTypeDao1 = ReaderTypeDaoImpl.getReaderTypeDao();
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();
            System.out.println("目前所拥有的罚金类型");
            Set<String> deleteReaderID = getDeleteReaderID();
            List<String> readerid = YouTool.change(deleteReaderID);
            Integer typeIndex = MyTool.getTypeIndex(readerid.size());
            String readerID = readerid.get(typeIndex);
//            System.out.println("输入你要删除的读者编号");
//            String readerID = YouTool.readerID();
//            boolean b = judgmentDelete(readerID);
//            if (b == false) {
//                System.out.println("无此类型的读者编号，请先前往添加");
//                System.out.println();
//                System.out.println("-----------------------------");
//                return;
//            }
            deleteSuccess(readerID);
            Druid.colseResource(connection,null,null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    public void deleteSuccess(String readerID) throws IOException {
        String file = "src//src//File//fineType.txt";

        Properties properties = new Properties();
        FileReader fileReader = new FileReader(file);
        properties.load(fileReader);
        properties.remove(readerID);

        FileWriter fileWriter = new FileWriter(file);
        properties.store(fileWriter, null);
        System.out.println("删除成功");
        fileReader.close();
        fileWriter.close();


    }
    public static String returnFine(String readerID) {
        Properties properties = new Properties();
        //该读者是否有罚金类型
        FileReader fr = null;
        try {
            fr = new FileReader("src//src//File//fineType.txt");
            properties.load(fr);
            Set<String> strings = properties.stringPropertyNames();

            for (String key : strings) {
                if ((readerID.equals(key))) {
                    String property = properties.getProperty(key);
                    return  property;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "1";
    }
    public boolean judgmentDelete(String readerID) {
        Properties properties = new Properties();
        //该读者是否有罚金类型
        FileReader fr = null;
        try {
            fr = new FileReader("src//src//File//fineType.txt");
            Set<String> strings = properties.stringPropertyNames();
            for (String key : strings) {
                System.out.println(key);
                return readerID.equals(key);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void update() {

        try {

            String file = "src//src//File//fineType.txt";
            System.out.println("输入你要修改的罚金类型");
            getAll();
            System.out.println("-----------------------");
            String readerID = YouTool.readerID();
            //该读者不存在
            boolean b = judgmentExist(readerID);
            if (b == false) {
                System.out.println("无此类型的读者编号,无法修改，请先前往添加");
                System.out.println();
                System.out.println("-----------------------------");
                return;
            }
            System.out.println("输入你要设置的新的罚金");
            int fine = TSUtility.readInt();


            Properties pro = new Properties();
            FileReader fileReader = new FileReader(file);
            pro.load(fileReader);
            pro.setProperty(readerID, String.valueOf(fine));


            FileWriter fileWriter = new FileWriter(file);
            pro.store(fileWriter, null);

            System.out.println("修改成功");
            fileWriter.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getAll() {
        Properties prop = new Properties();
        try {
            FileReader fr = new FileReader("src//src//File//fineType.txt");
            prop.load(fr);
            fr.close();
            System.out.println("读者编号=读者罚金:");
            System.out.println(prop);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Set<String> getDeleteReaderID( ) {
        Properties prop = new Properties();
        try {
            FileReader fr = new FileReader("src//src//File//fineType.txt");
            prop.load(fr);
            fr.close();
            System.out.println("读者编号=读者罚金:");
            int i =1;
            Set<String> strings =  prop.stringPropertyNames();
            for(String readerid :strings){

                String fine = prop.getProperty(readerid);
                System.out.println(i+"----"+readerid+","+fine);
                i++;

            }
            return strings;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
   return null; }

    public int jundment() {
        Properties prop = new Properties();
        try {
            FileReader fr = new FileReader("src//src//File//fineType.txt");
            prop.load(fr);
            fr.close();
            return prop.size();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        }
        return 0;
    }

    public static void main(String[] args) {

    }
}

